home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 80 / CD Actual 80 Julio-Agosto 2003.iso / Linux / LinuxGazette / lg / issue25 / tkdial < prev    next >
Encoding:
Tcl/Tk script  |  2002-08-14  |  3.4 KB  |  137 lines

  1. #!/usr/bin/wish -f
  2. #
  3. # Linux dialer. Shows on/off line, time on-line, and
  4. # pppd process identifier.
  5. #
  6. # (c) 1998 (GPL) Martin Vermeer
  7. #
  8. # User configurable:
  9. #----------------------------------------
  10. set path(pidof) /sbin/pidof
  11. set path(pppd) /usr/sbin/pppd
  12. set path(whoami) /usr/bin/whoami)
  13. #----------------------------------------
  14. # End user config
  15.  
  16. set seconds 0
  17. set minutes 0
  18. label  .counter -text "0:00" -relief sunken
  19.  
  20. ## Test for root.
  21. #
  22. ## Not needed if pppd suid root and /etc/ppp files 
  23. ## "options" and connect/disconnect scripts 
  24. ## (but NOT the "secrets" file!!! world readable
  25. ## (or readable to a group you belong to)
  26. #
  27. # catch "exec $path(whoami)" result
  28. # if {$result != "root"} {
  29. #     set labeltext "Run me as ROOT!"
  30. #     after 10000 exit 
  31. #}
  32.  
  33. # showpid -- routine to get and show process id, keep track
  34. #            of pppd link up/down status, and reset time
  35. #            counter if down
  36. #
  37. proc showpid {} {
  38.     global pid labeltext status seconds minutes path
  39.  
  40.     if [catch "exec $path(pidof) pppd" pid] {set pid -1}
  41.     if {$pid != -1} {
  42.         set labeltext "Link pid: ${pid}"
  43.         set status 1
  44.     } else {
  45.         set labeltext "Link Down"
  46.         set status 0; set seconds 0; set minutes 0
  47.     } 
  48. }
  49.  
  50. # tick -- routine to increment clock by 5 seconds every
  51. #         5 seconds, and minutes every minute.
  52. #         Don't be on-line over an hour :-)
  53. #
  54. proc tick {} {
  55.     global minutes seconds status pid labeltext
  56.     if $status {
  57.         after 5000 tick
  58.         incr seconds 5
  59.         if {$seconds >= 60} { set seconds 0; incr minutes }
  60.     } 
  61.     after 5000 showpid
  62.     .counter config -text [format "%d:%02d" $minutes $seconds]
  63. }
  64.  
  65. # Find out if there was an old pppd process running:
  66. #
  67. if [catch "exec $path(pidof) pppd" pid] {set pid -1}
  68. if {$pid != -1} {
  69.     set labeltext "Old Link ${pid}"
  70.     set status 1
  71.     tick
  72. } else {
  73.     set labeltext "Link Down"
  74.     set status 0
  75. }
  76.  
  77. #
  78. # Downcmd -- command to bring down the pppd link
  79. #
  80. proc Downcmd {} {
  81. global status path
  82.     # Get the pids of pppd processes into res:
  83.     if [catch "exec $path(pidof) pppd" res] {set res -1} 
  84.     if {$res != -1} {
  85.         # Some pppd process running; go get'em
  86.         catch "exec kill -9 $res" result
  87.         # Debug code:
  88.         # puts "pid=$res result=$result"
  89.     }
  90.     # Necessary to update $status to realistic value:
  91.     showpid
  92.     # await kill command to take effect:
  93.     while {$status == 1} showpid   
  94.     # don't come out until really down
  95. }
  96.  
  97. # Upcmd -- Command to bring up the pppd link
  98. #          and start the clock ticking
  99. #
  100. proc Upcmd {} {
  101.     global status path seconds minutes
  102.     # start up pppd:
  103.     if [catch "exec $path(pidof) pppd" res] {set res -1}
  104.     if {$res == -1} {
  105.         # no pppd process running yet; start one
  106.         catch "exec /usr/sbin/pppd"
  107.         set status 1
  108.     # start clock running
  109.     set seconds 0
  110.         set minutes 0
  111.         tick
  112.     }
  113.     if {$res > 0} { 
  114.         set labeltext "Old Link: ${res}" 
  115.         set status 1
  116.     }
  117. }
  118.  
  119. #
  120. # Define the widgets:
  121. #
  122. label        .name -textvariable labeltext 
  123. radiobutton  .up   -text "Up"   -variable status -value 1 -command Upcmd
  124. radiobutton  .down -text "Down" -variable status -value 0 -command Downcmd
  125. button       .quit -text "Quit" -command exit
  126.  
  127. #
  128. # Lay out grid geometry:
  129. #
  130. grid .name     -row 0 -column 0 -columnspan 2 -sticky "ew"
  131. grid .up       -row 1 -column 1 -sticky "w"
  132. grid .down     -row 2 -column 1 -sticky "w"
  133. grid .quit     -row 2 -column 0 
  134. grid .counter  -row 1 -column 0 -sticky "ns"
  135.  
  136.